﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using Moq;

namespace MvcWiki.Test.Repository.FileSystem.WikiNamespaceFileSystemRepository
{
    public class InsertEntityFacts
    {
        [Fact]
        public void Checks_Whether_Namespace_Directory_Exists()
        {
            var repositoryFactory = new Mock<MvcWiki.Repository.IWikiRepositoryFactory>();
            var wikiMarkupProvider = new Mock<MvcWiki.Markup.IWikiMarkupProvider>();
            var unitOfWork = new Mock<MvcWiki.Repository.IUnitOfWork>();

            var fileSystem = new Mock<MvcWiki.Repository.FileSystem.IFileSystemWrapper>();
            fileSystem.Setup(x => x.GetDirectories("c:\\path", "Namespace---*")).Returns(new string[] { "c:\\path\\Namespace---9f878f77-21bb-4fb1-a68e-3cd2c61201cc" });

            var wikiNamespaceRepository = new MvcWiki.Repository.FileSystem.WikiNamespaceFileSystemRepository(repositoryFactory.Object, wikiMarkupProvider.Object, unitOfWork.Object, "c:\\path", fileSystem.Object);

            var wikiNamespace = new MvcWiki.Model.WikiNamespace
            {
                Name = "Namespace",
                Markup = new MvcWiki.Model.WikiMarkup()
            };

            wikiNamespaceRepository.InsertEntity(wikiNamespace);

            fileSystem.VerifyAll();
        }

        [Fact]
        public void Checks_Whether_Namespace_File_Exists()
        {
            var repositoryFactory = new Mock<MvcWiki.Repository.IWikiRepositoryFactory>();
            var wikiMarkupProvider = new Mock<MvcWiki.Markup.IWikiMarkupProvider>();
            var unitOfWork = new Mock<MvcWiki.Repository.IUnitOfWork>();

            var fileSystem = new Mock<MvcWiki.Repository.FileSystem.IFileSystemWrapper>();
            fileSystem.Setup(x => x.GetDirectories("c:\\path", "Namespace---*")).Returns(new string[] { "c:\\path\\Namespace---9f878f77-21bb-4fb1-a68e-3cd2c61201cc" });
            fileSystem.Setup(x => x.FileExists("c:\\path\\Namespace---9f878f77-21bb-4fb1-a68e-3cd2c61201cc\\wikinamespace.xml")).Returns(false);

            var wikiNamespaceRepository = new MvcWiki.Repository.FileSystem.WikiNamespaceFileSystemRepository(repositoryFactory.Object, wikiMarkupProvider.Object, unitOfWork.Object, "c:\\path", fileSystem.Object);

            var newWikiNamespace = new MvcWiki.Model.WikiNamespace
            {
                Name = "Namespace",
                Markup = new MvcWiki.Model.WikiMarkup()
            };

            wikiNamespaceRepository.InsertEntity(newWikiNamespace);

            fileSystem.VerifyAll();
        }

        [Fact]
        public void Creates_Namespace_Directory_If_Does_Not_Exist()
        {
            var newWikiNamespace = new MvcWiki.Model.WikiNamespace
            {
                Name = "Namespace",
                Markup = new MvcWiki.Model.WikiMarkup()
            };

            var repositoryFactory = new Mock<MvcWiki.Repository.IWikiRepositoryFactory>();
            var wikiMarkupProvider = new Mock<MvcWiki.Markup.IWikiMarkupProvider>();
            var unitOfWork = new Mock<MvcWiki.Repository.IUnitOfWork>();

            var fileSystem = new Mock<MvcWiki.Repository.FileSystem.IFileSystemWrapper>();
            fileSystem.Setup(x => x.GetDirectories("c:\\path", "Namespace---*")).Returns(new string[] { });
            fileSystem.Setup(x => x.CreateDirectory(String.Format("c:\\path\\Namespace---{0}", newWikiNamespace.Id)));

            var wikiNamespaceRepository = new MvcWiki.Repository.FileSystem.WikiNamespaceFileSystemRepository(repositoryFactory.Object, wikiMarkupProvider.Object, unitOfWork.Object, "c:\\path", fileSystem.Object);

            wikiNamespaceRepository.InsertEntity(newWikiNamespace);

            fileSystem.VerifyAll();
        }

        [Fact]
        public void Throws_Exception_If_Namespace_File_Exists()
        {
            var repositoryFactory = new Mock<MvcWiki.Repository.IWikiRepositoryFactory>();
            var wikiMarkupProvider = new Mock<MvcWiki.Markup.IWikiMarkupProvider>();
            var unitOfWork = new Mock<MvcWiki.Repository.IUnitOfWork>();

            var fileSystem = new Mock<MvcWiki.Repository.FileSystem.IFileSystemWrapper>();
            fileSystem.Setup(x => x.GetDirectories("c:\\path", "Namespace---*")).Returns(new string[] { "c:\\path\\Namespace---9f878f77-21bb-4fb1-a68e-3cd2c61201cc" });
            fileSystem.Setup(x => x.FileExists("c:\\path\\Namespace---9f878f77-21bb-4fb1-a68e-3cd2c61201cc\\wikinamespace.xml")).Returns(true);

            var wikiNamespaceRepository = new MvcWiki.Repository.FileSystem.WikiNamespaceFileSystemRepository(repositoryFactory.Object, wikiMarkupProvider.Object, unitOfWork.Object, "c:\\path", fileSystem.Object);

            var newWikiNamespace = new MvcWiki.Model.WikiNamespace
            {
                Name = "Namespace",
                Markup = new MvcWiki.Model.WikiMarkup()
            };

            Assert.Throws<MvcWiki.Exceptions.NamespaceAlreadyExistsException>(
                () => wikiNamespaceRepository.InsertEntity(newWikiNamespace));
        }

        [Fact]
        public void Writes_Namespace_Xml_File_Exists()
        {
            var wikiNamespace = new MvcWiki.Model.WikiNamespace
            {
                Name = "Namespace",
                Markup = new MvcWiki.Model.WikiMarkup { Name = "WikiMarkup1" }
            };

            var repositoryFactory = new Mock<MvcWiki.Repository.IWikiRepositoryFactory>();
            var wikiMarkupProvider = new Mock<MvcWiki.Markup.IWikiMarkupProvider>();
            var unitOfWork = new Mock<MvcWiki.Repository.IUnitOfWork>();

            var fileSystem = new Mock<MvcWiki.Repository.FileSystem.IFileSystemWrapper>();
            fileSystem.Setup(x => x.GetDirectories("c:\\path", "Namespace---*")).Returns(new string[] { "c:\\path\\Namespace---9f878f77-21bb-4fb1-a68e-3cd2c61201cc" });
            fileSystem.Setup(x => x.FileExists("c:\\path\\Namespace---9f878f77-21bb-4fb1-a68e-3cd2c61201cc\\wikinamespace.xml")).Returns(false);
            fileSystem.Setup(x => x.WriteToFile("c:\\path\\Namespace---9f878f77-21bb-4fb1-a68e-3cd2c61201cc\\wikinamespace.xml", "<wikinamespace><wikimarkup>WikiMarkup1</wikimarkup></wikinamespace>"));

            var wikiNamespaceRepository = new MvcWiki.Repository.FileSystem.WikiNamespaceFileSystemRepository(repositoryFactory.Object, wikiMarkupProvider.Object, unitOfWork.Object, "c:\\path", fileSystem.Object);

            wikiNamespaceRepository.InsertEntity(wikiNamespace);

            fileSystem.VerifyAll();
        }
    }
}